<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属物业" prop="propertyId">
        <el-select v-model="queryParams.propertyId" placeholder="请选择物业" size="mini"  @change="selectEstate">
          <el-option v-for="property in propertyListOptions"
                     :key="property.id"
                     :label="property.propertyName"
                     :value="property.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="所属小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择小区" style="width:140px;" size="mini" filterable clearable>
          <el-option v-for="estate in estatesOptions"
                     :key="estate.id"
                     :label="estate.estateName"
                     :value="estate.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:patrolPoint:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所在物业" align="center" prop="propertyName" />
      <el-table-column label="所在小区" align="center" prop="estateName" />
      <el-table-column label="巡更点名称" align="center" prop="patrolTitle" />
      <el-table-column label="二维码下载" align="center" width="230">
        <template slot-scope="scope">
          <el-popover placement="right" width="400" trigger="hover">
            <el-image style="width: 380px; height: 380px" :src="scope.row.qrCode"  fit="fill" />
            <i class="el-icon-picture" slot="reference"/>
          </el-popover>
          <el-button size="mini" type="text" icon="el-icon-download"
                     @click="downloadImg(scope.row)">{{scope.row.patrolTitle }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="标准模板" align="center" prop="surveyStandardName">
        <template slot-scope="scope">
          <el-button size="mini" type="text"
                     @click="surveyStandard(scope.row)">{{scope.row.surveyStandardName }}</el-button>
        </template>
      </el-table-column>

      <el-table-column label="内容模板" align="center" prop="surveyTemplateName">
        <template slot-scope="scope">
          <el-button size="mini" type="text"
                     @click="surveyTemplate(scope.row)">{{scope.row.surveyTemplateName }}</el-button>
        </template>
      </el-table-column>.
      <el-table-column label="备注信息" align="center" prop="remark" />
<!--      <el-table-column label="状态" align="center" prop="state">-->
<!--        <template slot-scope="scope">-->
<!--          <div>-->
<!--            <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'info'">停用</el-tag>-->
<!--            <el-tag v-else style="cursor: pointer" :type=" '' ">正常</el-tag>-->
<!--          </div>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="状态" align="center" key="state">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state"
                     :active-value="1"
                     :inactive-value="0"
                     @change="handleChangeSaleOut(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-picture" @click="resetQrCode(scope.row)"
                     v-hasPermi="['module:patrolPoint:edit']">生成二维码</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:patrolPoint:edit']">修改</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"-->
<!--                     v-hasPermi="['module:patrolPoint:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改巡更点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span = 11>
            <el-row>
              <el-col :span="22">
                <el-form-item label="所属物业" prop="propertyId">
                  <el-select v-model="form.propertyId" placeholder="请选择物业" size="mini" style="width: 100%;"  filterable clearable @change="selectEstate">
                    <el-option v-for="property in propertyListOptions"
                               :key="property.id"
                               :label="property.propertyName"
                               :value="property.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="所属小区" prop="estateId">
                  <el-select v-model="form.estateId" placeholder="请选择小区" style="width: 100%;" size="mini" filterable clearable>
                    <el-option v-for="estate in estatesOptions"
                               :key="estate.id"
                               :label="estate.estateName"
                               :value="estate.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="巡更点名" prop="patrolTitle">
                  <el-input v-model="form.patrolTitle" placeholder="请输入巡更点名称" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="内容模板" prop="surveyTemplateId">
                  <el-select v-model="form.surveyTemplateId" placeholder="请选择内容模板" filterable clearable size="mini" style="width:100%">
                    <el-option v-for="su in surveyTemplateList"
                               :key="su.id"
                               :label="su.questionTitle"
                               :value="su.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="标准模板" prop="surveyStandardId">
                  <el-select v-model="form.surveyStandardId" placeholder="请选择内容模板" filterable clearable size="mini" style="width:100%">
                    <el-option v-for="su in surveyStandardList"
                               :key="su.id"
                               :label="su.questionTitle"
                               :value="su.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="任务类型" prop="pointQrInfoId">
                  <el-select v-model="form.pointQrInfoId" placeholder="请选择通用点" filterable clearable size="mini" style="width:100%">
                    <el-option v-for="qr in qrList"
                               :key="qr.id"
                               :label="qr.typeName"
                               :value="qr.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="备注信息" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入备注信息" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="状态" prop="state" >
                  <el-radio-group v-model="form.state" size="mini">
                    <el-radio :value="0" :label="0">停用</el-radio>
                    <el-radio :value="1" :label="1">正常</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="13">
            <el-col :span="24">
              <div style="width:100%;height:400px;">
                <TianDiTu :zoom="12" :label-title="form.patrolTitle"
                        :default-lat="parseFloat(form.latitude)" :default-lng="parseFloat(form.longitude)"
                        @pickLngLat="pickLngLat"></TianDiTu>
              </div>
            </el-col>
            <el-col :span="24">
              <el-form-item label="经纬度" prop="lng">
                <el-input v-model="form.lng" placeholder="请输入经度" style="width:47%" size="mini"/>
                <el-input v-model="form.lat" placeholder="请输入纬度" style="width:47%" size="mini"/>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>

    <!--标准模板-->
    <el-dialog :title="titleSurvey" :visible.sync="openSurvey" width="500px" append-to-body>
      <p v-html="info">{{info}}</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelSurvey" size="mini">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listPoint, getPoint, delPoint, addPoint, updatePoint, exportPoint,resetQrCode,updatePointState } from "@/api/module/event/patrol/point";
import Template from "@/views/module/wx/template.vue";

import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listPropertyNoPage} from "@/api/module/property/property";
import {listSurvey} from "@/api/module/survey/survey";
import {listAllEstate} from "@/api/module/estate/estate";
import {listQrs} from "@/api/module/event/point/qr";
import {listInfos} from "@/api/module/event/point/qrInfo";
import {updateTerminalState} from "@/api/module/event/terminal/terminals";

export default {
  name: "Point",
  components: {Template},
  data() {
    return {
      map:undefined,
      marker: undefined,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡更点表格数据
      pointList: [],
      tipinput:'',
      titleSurvey: "",
      info:undefined,
      propertyListOptions:[],
      estatesOptions:[],
      surveyTemplateList: [],
      surveyStandardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openTemplate: undefined,
      openSurvey: undefined,
      qrList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParamsEstate: {
        propertyId: undefined,
      },
      queryParamQr:{
        eventType:2,
      },
      queryParamsSurvey: {
        pageNum: 1,
        pageSize: 100,
        propertyId: undefined,
        estateId: undefined,
        surveyType:undefined,
        functionType:2,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getPropertyList();
    this.getList();
  },
  methods: {
    /** 查询巡更点列表 */
    getList() {
      this.loading = true;
      listPoint(this.queryParams).then(response => {
        this.pointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    pickLngLat(e){
      console.log('getLngLat e === ', e)
      this.form.lng = e.lng.toFixed(6);
      this.form.lat = e.lat.toFixed(6);
    },
    handleChangeSaleOut(row) {
      let text = row.state === 0 ? "启用" : "停用";
      this.$confirm('确认要修改"' + text + '"的状态吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return updatePointState(row.id, row.state);
      }).then(() => {
        this.msgSuccess("修改 " + text + "成功");
        this.getList();
      }).catch(function () {
        row.state = row.state === "0" ? "1" : "0";
      });
    },
    surveyStandard(row){
      this.openSurvey=true;
      this.info = row.surveyStandardinfo;
      this.titleSurvey = "标准模板";
    },
    cancelSurvey() {
      this.openSurvey = false;
      this.info=undefined;
    },
    surveyTemplate(row){
      this.openSurvey=true;
      this.info = row.surveyTemplateinfo;
      this.titleSurvey = "内容模板";
    },
    getQrPointList(){
      listInfos(this.queryParamQr).then(response => {
        this.qrList = response;
        this.loading = false;
      });
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    getSurveyTemplateList() {
      this.queryParamsSurvey.surveyType = 1;
      listSurvey(this.queryParamsSurvey).then(response => {
        this.surveyTemplateList = response.rows;
      });
    },
    getSurveyStandardList() {
      this.queryParamsSurvey.surveyType = 2;
      listSurvey(this.queryParamsSurvey).then(response => {
        this.surveyStandardList = response.rows;
      });
    },
    selectEstate(val){
      this.queryParamsEstate.propertyId = val;
      this.getEstateList();
    },
    getEstateList() {
      listAllEstate(this.queryParamsEstate).then(response => {
        console.log('getEstateList',response.data);
        this.estatesOptions = response.data;
      });
    },
    downloadImg(row){
      window.location.href = process.env.VUE_APP_BASE_API + "/common/download/patrol/" + row.id;
    },
    resetQrCode(row){
      var data = {
        id: row.id
      };
      resetQrCode(data).then(response => {
        if(response.code === 200){
          this.msgSuccess("生成成功");
          this.getList();
        }else{
          this.msgError("生成失败");
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        patrolTitle: undefined,
        surveyTemplateId: undefined,
        surveyStandardId: undefined,
        lng: undefined,
        lat: undefined,
        remark: undefined,
        state: undefined,
        createTime: undefined,
        pointQrInfoId: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      let that = this;
      this.reset();
      this.getPropertyList();
      this.getQrPointList();

      this.getSurveyTemplateList();
      this.getSurveyStandardList();
      this.open = true;
      this.title = "添加巡更点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let that = this;
      this.loading = true;
      this.reset();
      this.getPropertyList();
      this.getQrPointList();
      this.getSurveyTemplateList();
      this.getSurveyStandardList();
      const id = row.id || this.ids;
      getPoint(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改巡更点";
        this.queryParamsEstate.propertyId = this.form.propertyId;
        this.getEstateList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePoint(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addPoint(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除巡更点编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delPoint(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有巡更点数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportPoint(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>


<style lang="scss" scoped>
.mapSelect {
  position: absolute;
  top: 5px;
  right: 1px;
  background: #fff none repeat scroll 0 0;
  border: 1px solid #ccc;
  margin: 10px auto;
  padding:6px;
  font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";
  font-size: 14px;
}
</style>

<style lang="scss" >
.amap-sug-result{
  z-index: 99999!important;
}
</style>
